#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2018/1/14 0014 15:45
# @Author  : Arliki
# @File    : url_test.py
from urllib import request, parse
import re


def simulation_get():
    print('_____simulation_get________')
    req = request.Request('http://www.sohu.com/a/216447055_495793')
    # req.add_header('User-Agent', 'Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25') #苹果6
    req.add_header('User-Agent',
                   'Mozilla/5.0 (Linux; U; Android 4.3; en-us; SM-N900T Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30')  # 安卓
    with request.urlopen(req) as f:
        data = f.read()
        print('sss', f.status, f.reason)
        for k, v in f.getheaders():
            print('%s,,%s' % (k, v))
            # print('data:::',data.decode('utf-8'))


def simulation_post():
    print('_____simulation_post________')
    email = '123'
    passwd = '123'
    login_data = parse.urlencode([
        ('username', email),
        ('password', passwd)
    ])
    req = request.Request(
        'http://reg.163.com/logins.jsp?type=1&product=mail163&url=http://entry.mail.163.com/coremail/fcg/ntesdoor2?lightweight%3D1%26verifycookie%3D1%26language%3D-1%26style%3D1')
    req.add_header('Origin', 'http://reg.163.com')
    req.add_header('User-Agent',
                   'Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25')
    with request.urlopen(req, data=login_data.encode('utf-8')) as f:
        for k, v in f.getheaders():
            print(k, "+++", v)
        # d=open('test.html','w+',encoding='utf8')
        # d.write(re.sub(r'[\s\r\n]{2,}','',f.read().decode('utf-8')))
        # d.close()
        print('data:', f.read().decode('utf-8'))


if __name__ == '__main__':
    # simulation_get()
    # simulation_post()
    while True:
        with open('test.json', 'a+', encoding='utf-8') as f:
            import time

            f.write("\n" + str(time.time()))
            time.sleep(1)
